#include<iostream>
#define MAX 51

using namespace std;

int ncases;
int train[MAX];
int swaps;
int n;
 
void sort() {
   for(int i=1; i<n; i++) {
      for(int j=0; j<n-1; j++) {
         if(train[j]>train[j+1]) {
            int temp = train[j];
            train[j] = train[j+1];
            train[j+1] = temp;
            swaps++;
         }
      }
   }   
}

void read() {
   cin >> ncases;
   for(int i=0; i<ncases; i++) {
      swaps = 0;
      cin >> n;
      for(int j=0; j<n; j++) 
         cin >> train[j];
      sort();
      cout << "Optimal train swapping takes " << swaps << " swaps." << endl;
   }
}

int main() {
   read();
   return 0;
}
